#!/bin/bash

PROC_CHLIST="/opt/fonsview/NE/tvld/proc/tvld_proc"
PROC_HLS="/opt/fonsview/NE/ss/data/proc/rem_hls/used"
PROC_IPTV="/opt/fonsview/NE/ss/data/proc/rem/used_base"


if [ "TVLD" == "$1" ];
then
        if [ ! -n "$2" ] ;
        then
        echo -e "{\"data\":["
        n=0
        TVLD_NUM=`cat ${PROC_CHLIST}/chlistu |grep channel |awk '{print $3}'`
        cat ${PROC_CHLIST}/chlistu |  awk '{if($1 ~ /^[0-9]+$/) print $3;}' | while read line
        do
            
            echo "{\"{#CONTENTID}\": \"$line\"}"
            n=$(($n+1))
            if [ $TVLD_NUM -gt $n ] ;then
                echo ","
            fi
        
    done
    echo -e "]}"
    else
        if [ "SINGAL" == "$2" ];
        then
            Signal_NUM=`cat ${PROC_CHLIST}/chlistu | grep $3 |awk '{print $1}'`
            Signal_STU=`cat ${PROC_CHLIST}/chlist | awk -v narv=${Signal_NUM} '{if($1==narv) print $8;}'`
            if [ !  $Signal_STU ]; 
	    then
                    echo "0"
	    else
            	if [ "Ok" == "$Signal_STU" ];
            	then
                	echo "0"
            	else
                	echo "1"
		fi
            fi
        fi
    fi

elif [ "HLS" == "$1" ];
then
    if [ ! -n "$2" ] ;
    then
        echo -e "{\"data\":["
        n=0
        TOTAL_NUM=`cat ${PROC_HLS} | grep used | awk '{print $2}'`
        HEAD_NUM=`expr $TOTAL_NUM + 1`
        cat ${PROC_HLS} | head -$HEAD_NUM | tail -$TOTAL_NUM | awk '{print $2}' | awk -F "@" '{print $2}' | while read line
        do
                echo "{\"{#CONTENTID}\": \"$line\"} "
                        n=$(($n+1))
                        if [ $TOTAL_NUM -gt $n ] ;then
                                echo ","
                        fi    
        done
        echo -e "]}"
    
        elif [ "IR" == "$2" ];
            then
                    PAUSE=`cat ${PROC_HLS} | grep $3 | awk '{print $9}' | awk -F ":" '{print $2}'`
            if [ !  $PAUSE ]; then
                echo ""
            else
                if [ 0 -eq $PAUSE ]; then
                                    echo "0"
                            elif [ 0 -lt $PAUSE ]; then
                                    echo "1"
                fi
                    fi
        
        elif [ "UO" == "$2" ];
            then
                    UNORDER=`cat ${PROC_HLS} | grep $3 | awk '{print $8}' | awk -F ":" '{print $2}'`
                        if [ !  $UNORDER ]; then
                                echo ""
            else
                        if [ 0 -eq $UNORDER ]; then
                                  echo "0"
                            elif [ 0 -lt $UNORDER ]; then
                                 echo "1"
                            fi
            fi
            
                
    fi
elif [ "IPTV" == "$1" ];
then
    if [ ! -n "$2" ];
    then
            echo -e "{\"data\":["
            n=0
            IPTV_NUM=`cat ${PROC_IPTV} | grep total | awk '{print $2}'`
            cat ${PROC_IPTV} | grep BASE | awk '{print $4}'  | while read line2
            do
                        echo "{\"{#CONTENTID}\": \"$line2\"} "
                        n=$(($n+1))
                        if [ $IPTV_NUM -gt $n ] ;then
                                echo ","
                        fi
            done
            echo -e "]}"
    

        elif [ "IR" == "$2" ];
    then
        INTERUPT=`cat ${PROC_IPTV} | grep $3 | awk '{print $12}' | awk -F ":" '{print $2}'`
                if [ "$INTERUPT" == "" ]; then
                    echo "0"
        else
            if [ 0 -eq $INTERUPT ]; then
                echo "0"
            elif [ 0 -lt $INTERUPT ]; then
                echo "1"
            fi
        fi
        elif [ "UO" == "$2" ];
    then
        UNORDER=`cat ${PROC_IPTV} | grep $3 | awk '{print $11}' | awk -F ":" '{print $2}'`
                if [ !  $UNORDER ]; then
                        echo "0"
        else
                    if [ 0 -eq $UNORDER ]; then
                            echo "0"
                    
            elif [ 0 -lt $UNORDER ]; then
                            echo "1"
                    fi
        fi
    
    fi
fi
